<?php
namespace app\home\Controller;
use think\Controller;
use think\Request;
use think\Db;
// use app\index\model\index;
class Cart extends Controller
{
	// 购物车
    public function submit()
    {
        $u_id = cookie("u_id");
        // 用户购物车商品
        $sql = "select * from man_shopping_cart,man_goods where man_shopping_cart.g_id=man_goods.g_id and u_id=$u_id";
        $cartData = Db::query($sql);

        foreach ($cartData as $key => $value) {
            // 猜你喜欢
            $sql = "select * from man_goods where g_id='$value[g_id]' order by g_addtime desc limit 1";
            $goodsDataFx = Db::query($sql);
            $goodsDataFx1[] = $goodsDataFx[0];
        }

        $typeData = $this->alltype();
        return view('submit',[
            'typeData'=>$typeData,
            'cartData'=>$cartData,
            'goodsDataFx'=>$goodsDataFx1
            ]
        );
    }
    // 确认订单
    public function submitpath()
    {
        // 用户购物车商品
        $sql = "select * from man_shopping_cart,man_goods where man_shopping_cart.g_id=man_goods.g_id and u_id=1";
        $cartData = Db::query($sql);



        // 省市级联动
        if($this->request->isAjax()){
            $area_id = input("get.area_id");
            $sql = "select * from man_area where area_parent_id=$area_id";
            $areaData = Db::query($sql);
            $return = array('areaData'=>$areaData);
            echo json_encode($return);
            die;
        }
        $sql = "select * from man_area where area_parent_id=0 and area_name='北京'";
        $areaData = Db::query($sql);
        
        $typeData = $this->alltype();
        return view('submitpath',['typeData'=>$typeData,'areaData'=>$areaData,'cartData'=>$cartData]);
    }
    // 提交订单成功
    public function truesubmit()
    {
        $typeData = $this->alltype();
        return view('truesubmit',['typeData'=>$typeData]);
    }

    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }
    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }

}